package com.yhsoft;

import com.yhsoft.common.utils.Md5Utils;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 以下参数是接口调用的必须参数：
 * 参数名	数值类型	参数值及说明
 * partnerId	Long	渠道 id
 * code	String	code
 * timestamp	Long	时间戳（13 位数据值，毫秒）30 秒有效时间
 * sign	String	签名
 * 其中签名的算法为：
 * <p>
 * string = partnerId + timestamp + code;
 * sign = Md5(string(转小写)+ secretKey);
 * <p>
 * <p>
 * 测试环境：
 * partnerId :   100219
 * code:            q80JSPZdmNPI
 * secretKey :   xcSjqJ9Ub4Tu
 * <p>
 * <p>
 * <p>
 * 使用httpclient工具类型同步数据
 */
public class FuSiLingFlimApiTest1 {

    private final StringBuilder testApi = new StringBuilder("http://testopenapi.movie.fsiling.com");
    private final CloseableHttpClient httpClient = HttpClients.createDefault();
    private final String partnerId = "100219";
    private final String code = "q80JSPZdmNPI";
    private final String secretKey = "xcSjqJ9Ub4Tu";
    private final long timestamp = System.currentTimeMillis();
    private final String sign = Md5Utils.getMD5Code((partnerId + timestamp + code).toLowerCase() + secretKey).toLowerCase();

    /**
     * 座位图数据接口(实时调用)
     * showId	是	String	场次ID
     */
    @Test
    public void movieSeat() throws IOException {
        testApi.append("/openapi/movie/seat/map");
        List<BasicNameValuePair> nameValuePairs = new ArrayList<>();
        nameValuePairs.add(new BasicNameValuePair("showId", "0700005162210"));


        HttpPost httpPost = modifyCommPostHeader(nameValuePairs);
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        System.out.println("response >>> : " + response);
        System.out.println(String.format("请求结果：%s", EntityUtils.toString(entity, StandardCharsets.UTF_8)));

        response.close();
    }

    /**
     * 座位图锁座接口
     * showId	是	String	场次ID
     * mobile	是	String	手机号码
     * seatNames	是	String	L排07座|L排08座
     * seatIds	是	String	672446|672447
     * totalMoney	否	Long	当前锁座电影票市场总额（单位分），传此参数则会强校验锁座调用方总金额是否福司令的锁座总金额是否一致，一致则锁座成功
     */
    @Test
    public void movieSeatLock() throws IOException {
        testApi.append("/openapi/movie/seat/lock");
        List<BasicNameValuePair> nameValuePairs = new ArrayList<>();
        nameValuePairs.add(new BasicNameValuePair("showId", "1232821024"));
        nameValuePairs.add(new BasicNameValuePair("mobile", "18888888888"));
        nameValuePairs.add(new BasicNameValuePair("seatNames", "9排2座"));
        nameValuePairs.add(new BasicNameValuePair("seatIds", "4985"));
        nameValuePairs.add(new BasicNameValuePair("totalMoney", "4500"));
        HttpPost httpPost = modifyCommPostHeader(nameValuePairs);

        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        System.out.println("response >>> : " + response);
        System.out.println(String.format("请求结果：%s", EntityUtils.toString(entity, StandardCharsets.UTF_8)));

        response.close();
    }

    /**
     * 座位图解锁接口
     *applyKey	是	String	锁座标识
     */
    @Test
    public void movieSeatUnLock() throws IOException {
        testApi.append("/openapi/movie/seat/unlock");
        List<BasicNameValuePair> nameValuePairs = new ArrayList<>();
        nameValuePairs.add(new BasicNameValuePair("applyKey", "100"));

        HttpPost httpPost = modifyCommPostHeader(nameValuePairs);
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        System.out.println("response >>> : " + response);
        System.out.println(String.format("请求结果：%s", EntityUtils.toString(entity, StandardCharsets.UTF_8)));

        response.close();
    }

    /**
     * 支付确认接口（出票接口）
     * partnerOrderNo	是	String	合作商(接入方)订单号
     * applyKey	是	String	锁座标识
     * isChangeSeats	否	String	是否支持调座，true: 允许调座，不传或其他值表示不允许调座
     */
    @Test
    public void payConfirm() throws IOException {
        testApi.append("/openapi/movie/order/pay-confirm");
        List<BasicNameValuePair> nameValuePairs = new ArrayList<>();
        nameValuePairs.add(new BasicNameValuePair("partnerOrderNo", "20231206000001"));
        nameValuePairs.add(new BasicNameValuePair("applyKey", "zfy_1701800154633_xh6q9jho1o"));
        nameValuePairs.add(new BasicNameValuePair("isChangeSeats", "true"));


        HttpPost httpPost = modifyCommPostHeader(nameValuePairs);
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        System.out.println("response >>> : " + response);
        System.out.println(String.format("请求结果：%s", EntityUtils.toString(entity, StandardCharsets.UTF_8)));

        response.close();
    }

    /**
     * 出票查询
     * partnerOrderCode	必须		String	合作商(接入方)订单号
     */
    @Test
    public void query() throws IOException {
        testApi.append("/openapi/movie/order/query");
        List<BasicNameValuePair> nameValuePairs = new ArrayList<>();
        nameValuePairs.add(new BasicNameValuePair("partnerOrderCode", "20231206000001"));


        HttpPost httpPost = modifyCommPostHeader(nameValuePairs);
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        System.out.println("response >>> : " + response);
        System.out.println(String.format("请求结果：%s", EntityUtils.toString(entity, StandardCharsets.UTF_8)));

        response.close();
    }

    @NotNull
    private HttpPost modifyCommPostHeader(List<BasicNameValuePair> nameValuePairs) throws UnsupportedEncodingException {
        System.out.println("请求地址： " + testApi);
        HttpPost httpPost = new HttpPost(testApi.toString());
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        nameValuePairs.add(new BasicNameValuePair("partnerId", partnerId));
        nameValuePairs.add(new BasicNameValuePair("code", code));
        nameValuePairs.add(new BasicNameValuePair("timestamp", String.valueOf(timestamp)));
        nameValuePairs.add(new BasicNameValuePair("sign", sign));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
        return httpPost;
    }
}
